from dataholder import dataholder
import logging


class colorholder():
    min_basic_colors=4
    colorlist=[]# filed with list of tuples, the first tuple in each inner list i a "basic color"
    def __init__(self,dh=dataholder()):
        self.dh=dh;
        nr_of_basic_colors=self.min_basic_colors+dh.ibyte0to15() #4-19 basic colors

        #Make a list ( c ) of lists (c1, c2 ...) with the basic color as the first in inner list. A color is a tulup
        for i in range(nr_of_basic_colors):
            versionlist=[]
            versionlist.append(self.create_basic_color())
            self.colorlist.append(versionlist)
            
        #Make 0-15 (byte) versions of each color
        for versionlist in self.colorlist:
            versionlist.extend(self.create_color_versions(versionlist[0]))
            

    def create_basic_color(self):
        #returns a basic color, based apon data from dataholder
        r=self.dh.ibytes(2)
        g=self.dh.ibytes(2)
        b=self.dh.ibytes(2)
        return (r,g,b)

    def black_or_white(self,colorpart,change):
        if colorpart+change>255: return 255-colorpart
        else: return 0-colorpart
        
    def get_signed_change(self,colorpart,maxchange):
        """ returns the change, if the color+change>255 or less then 0 the sign is ither changed or sett to the max/min depending on ..."""
        logging.debug("max=%s, part=%s",maxchange,colorpart)
        change=self.dh.get0to(maxchange*2) #get change (max change in each direction *2)
        change-=maxchange # now change =  +- maxchange
        #if we fall of the "edge" there is a 50/50 % chans that we change the sign or set the colorpart+change to be max 255 or min 0
        if(colorpart+change>255) or (colorpart+change<0): 
            if (self.dh.tof()):return self.black_or_white(colorpart,change)
            else: return -change
        else: return change
         
    def create_color_versions(self,basic_color):
        #return a list of 0-15 tuples (colors), that are versions of the ether the basic_color(first in list) or the last version
        #a version is basic_color (+-30 on each value) or the last color +-10
        nr_of_versions=self.dh.ibyte0to15() #nr of versions for this color
        retlist=[]
        fromlast=self.dh.tof()
        old_r,old_g,old_b=basic_color[0],basic_color[1],basic_color[2]
        for i in range(nr_of_versions):
            if (fromlast):
                r=old_r+self.get_signed_change(old_r,30)
                g=old_g+self.get_signed_change(old_g,30)
                b=old_b+self.get_signed_change(old_b,30)
                logging.debug("FROM LAST: rgb=%s,%s,%s",r,g,b)
            else:
                r=basic_color[0]+self.get_signed_change(basic_color[0],30)
                g=basic_color[1]+self.get_signed_change(basic_color[1],30)
                b=basic_color[2]+self.get_signed_change(basic_color[2],30)
                logging.debug("FROM BASIC: rgb=%s,%s,%s",r,g,b)
            retlist.append((r,g,b))
            old_r,old_g,old_b=r,g,b
        return retlist
        
    

if __name__ == "__main__":

    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)s %(levelname)s %(message)s',
                        filename='debug.log',
                        filemode='w')

    data=dataholder("Mebb")
    ch=colorholder(data)
    
    f=open("colorholder_test.html","w");
    f.write("<html><head></head><body><table>");
    for versionlist in ch.colorlist:
        f.write("<tr>")
        for color in versionlist:
            print color
            #colstr=  "bgcolor=\"rgb("  +   str(color[0])  +  "," +  str(color[1])  +  ","  +  str(color[2])  +  ")\""
            colstr="bgcolor=#" + hex(color[0])[-2:] + hex(color[1])[-2:] + hex(color[2])[-2:]
            f.write("<td "+colstr+">&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</td>\n")
        f.write("</tr>\n")
        print 
    f.write("</table></body></html>");
    f.close()
